/**
 * @(#) TrainingProxy.as
 *
 * @date		2009. 03. 19.
 * @version		1.0
 * @author		Park, Juneho
 * @description 가족정보 Proxy - Proxy
 * 				가족정보를 조회 한다.
 */
package com.skt.common_hr.training.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.training.TrainingFacade;
	import com.skt.common_hr.training.model.vo.TrainingVO;
	import com.skt.common_hr.training.model.vo.LeaveVO;
	import com.skt.common_hr.training.model.vo.MainContentsVO;
	import com.skt.common_hr.training.model.vo.PurposeVO;
	import com.skt.common_hr.training.model.vo.TargetVO;
	import com.skt.common_hr.training.model.vo.TrainingDetailVO;
	import com.skt.common_hr.training.model.vo.TrainingListVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class TrainingProxy extends CommonProxy
	{
		public static const NAME:String = "TrainingProxy";
		
		public var trainingTree:XMLListCollection;
		public var trainingList:ArrayCollection;
		
		public var trainingDetail:TrainingDetailVO;		//상세내역
		public var trainingTarget:TargetVO;				//대상
		public var trainingPurpose:PurposeVO;			//목적
		public var trainingMainContents:MainContentsVO;	//주요내용
		public var trainingLeave:LeaveVO;				//이수기준
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function TrainingProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function loadTrainingCourse():void
		{
			var param:Object = new Object();
			loadData("Z_HR_0075", param, resultTrainingCourse, faultTrainingCourse); 
		}
		

		public function loadDetail(code:String, startDt:Date, endDt:Date):void{
			var param:Object = new Object();
			param["I_OBJID"] = code;
			param["I_BEGDA"] = DateUtil.toBasicString(startDt);
			param["I_ENDDA"] = DateUtil.toBasicString(endDt);

			loadData("Z_HR_0076", param, resultCourseDetail, faultCourseDetail);
		}
		
		
		
		
		
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultTrainingCourse(event:ResultEvent):void
		{
			if(retCode==0) {  
				if(event.result.RTAB.length > 0){
					trainingTree = new XMLListCollection(parseTrainingTree(event.result.RTAB));
					trainingList = parseTrainingList(event.result.LTAB);
				}
				facade.sendNotification(TrainingFacade.LOAD_TRAINING_COURSE_COMPLETE, this);
			}
			else {
				facade.sendNotification(TrainingFacade.LOAD_TRAINING_COURSE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultTrainingCourse(event:FaultEvent):void
		{
			facade.sendNotification(TrainingFacade.LOAD_TRAINING_COURSE_FAILED, this);
		}
		

		
		/****
		 * 교육체계별 세부사항 로드 완료
		 * **/
		public function resultCourseDetail(event:ResultEvent):void
		{
			if(retCode==0) {  
				parseTrainingDetail(event.result.ITAB);
				parseTrainingTarget(event.result.TTAB1);
				parseTrainingPurpose(event.result.TTAB2);
				parseTrainingMainContents(event.result.TTAB3);
				parseTrainingLeave(event.result.TTAB4);
				facade.sendNotification(TrainingFacade.LOAD_DETAIL_COMPLETE, this);
			}
			else {
				facade.sendNotification(TrainingFacade.LOAD_DETAIL_FAILED, this);
			}
		}
		
		/****
		 * 교육체계별 세부사항 로드 실패
		 * **/
		private function faultCourseDetail(event:FaultEvent):void
		{
			facade.sendNotification(TrainingFacade.LOAD_DETAIL_FAILED, this);
		}

		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 가족관계 Value Object
		 */
		private function parseTrainingTree(srcArr:ArrayCollection):XMLList
		{
			var prevArr:Array = new Array();
			var root:XML = <node label={srcArr[0].STEXT} objid={srcArr[0].OBJID} level={srcArr[0].LEVEL} begda={srcArr[0].BEGDA} endda={srcArr[0].ENDDA} chasu={srcArr[0].CHASU} parent={srcArr[0].UPPEROBJ}/>;
			prevArr[root.@level] = root;
						
			for(var i:int=1; i<srcArr.length; i++)
			{
				var rec:Object = srcArr[i];
				var node:XML = <node label={rec.STEXT} objid={rec.OBJID} level={rec.LEVEL} begda={rec.BEGDA} endda={rec.ENDDA} chasu={rec.CHASU} parent={rec.UPPEROBJ}/>;
				
				if(prevArr[parseInt(rec.LEVEL)-1]!=null) {
					XML(prevArr[parseInt(rec.LEVEL)-1]).appendChild(node);
				}
				prevArr[node.@level] = node;
			}
			//trace(root.toXMLString());
			return new XMLList(root);
		}


		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 가족관계 Value Object
		 */
		private function parseTrainingList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:TrainingVO = new TrainingVO();
				vo.LCODE		= rec["LCODE"];			//경조 가족관계 텍스트
				vo.LTEXT		= rec["LTEXT"];			// 교육체계(L)코드
				vo.DCODE		= rec["DCODE"];			//
				vo.DTEXT		= rec["DTEXT"];			// 교육(D)코드
				vo.CHASU		= rec["CHASU"];			// 차수
				vo.BEGDA		= rec["BEGDA"];			// 시작일
				vo.ENDDA		= rec["ENDDA"];			// 종료일
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		/****
		 * 상세내용
		 * **/
		private function parseTrainingDetail(srcArr:ArrayCollection):void
		{
			for each(var rec:Object in srcArr) 
			{
				var vo:TrainingDetailVO = new TrainingDetailVO();
				vo.L_OBJID	= rec["L_OBJID"];		//교육체계(L)코드
				vo.L_STEXT	= rec["L_STEXT"];		//오브젝트 이름
				vo.D_OBJID	= rec["D_OBJID"];		//교육(D) 코드
				vo.D_STEXT	= rec["D_STEXT"];		//오브젝트 이름
				vo.BEGDA	= rec["BEGDA"];			//시작일
				vo.ENDDA	= rec["ENDDA"];			//종료일
				vo.PERIOD	= rec["PERIOD"];		//텍스트
				vo.R_OBJID	= rec["R_OBJID"];		//교육장소(R)코드
				vo.R_STEXT	= rec["R_STEXT"];		//오브젝트이름
				vo.U_OBJID	= rec["U_OBJID"];		//교육기관(U) 코드
				vo.U_STEXT	= rec["U_STEXT"];		//오브젝트 이름
				vo.TFORM	= rec["TFORM"];			//교육형태
				vo.TXTSING	= rec["TXTSING"];		//교육형태  텍스트
				vo.NDAYS	= rec["NDAYS"];			//일별 유효기간
				vo.NHOURS	= rec["NHOURS"];		//기간n시간
				vo.IKOST	= rec["IKOST"];			//내부가격
				vo.IWAER	= rec["IWAER"];			//통화키
				vo.IKOSTT	= rec["IKOSTT"];		//20자
				vo.KAPZ2	= rec["KAPZ2"];			//최적수용인원
				vo.MANAGER	= rec["MANAGER"];		//교육담당
				 
				this.trainingDetail = vo;
			}
		}
		
		/****
		 * 대상
		 * **/
		private function parseTrainingTarget(srcArr:ArrayCollection):void
		{
			var vo:TargetVO = new TargetVO();
			vo.TLINE = "";
			for each(var rec:Object in srcArr) 
			{
				vo.TABSEQNR	= rec["TABSEQNR"];			//교육체계(L)코드
				vo.TLINE	+= "\n" + rec["TLINE"];		//오브젝트 이름
			}
			vo.TLINE = strReplace(vo.TLINE);
			this.trainingTarget = vo;
		}
		
		/****
		 * 목적
		 * **/
		private function parseTrainingPurpose(srcArr:ArrayCollection):void
		{
			var vo:PurposeVO = new PurposeVO();
			vo.TLINE = "";
			for each(var rec:Object in srcArr) 
			{
				vo.TABSEQNR	= rec["TABSEQNR"];			//교육체계(L)코드
				vo.TLINE	+= "\n" + rec["TLINE"];		//오브젝트 이름
			}
			vo.TLINE = strReplace(vo.TLINE);
			this.trainingPurpose = vo;
		}
		
		/****
		 * 주요내용
		 * **/
		private function parseTrainingMainContents(srcArr:ArrayCollection):void
		{
			var vo:MainContentsVO = new MainContentsVO();
			vo.TLINE = "";
			for each(var rec:Object in srcArr) 
			{
				vo.TABSEQNR	= rec["TABSEQNR"];			//교육체계(L)코드
				vo.TLINE	+= "\n" + rec["TLINE"];		//오브젝트 이름
			}
			vo.TLINE = strReplace(vo.TLINE);
			this.trainingMainContents = vo;
		}
		
		/****
		 * 이수기준
		 * **/
		private function parseTrainingLeave(srcArr:ArrayCollection):void
		{
			var vo:LeaveVO = new LeaveVO();
			vo.TLINE = "";
			for each(var rec:Object in srcArr) 
			{
				vo.TABSEQNR	= rec["TABSEQNR"];			//교육체계(L)코드
				vo.TLINE	+= "\n" + rec["TLINE"];		//오브젝트 이름
			}
			vo.TLINE = strReplace(vo.TLINE);
			this.trainingLeave = vo;
		}
		private function strReplace(val:String):String{
			return val.substr(1, val.length);
		}
		
	}
}